#pragma once

#include "ColligereDatabaseItemBase.hpp"

namespace col {

/*! @brief A database event class
 *
 * Used to store information about a database event.
 **/
class DatabaseEventBase : public wxEvent 
{
public:
	DatabaseEventBase ( wxEventType commandType, wxInt32 id,
		const DatabaseItemBase *pItem = 0 );
	virtual ~DatabaseEventBase() { }

	bool Vetoed() const { return mVetoed; }
	void Veto ( bool veto = true ) { mVetoed = veto; }

	//! Overrides wxEvent::Clone().
	wxEvent *Clone() const { return ( new DatabaseEventBase ( *this ) ); }

	void SetRequestedItemId ( wxUint32 id ) { mReqItemId = id; }
	void SetRequestedItemParentId ( wxUint32 id ) { mReqItemParentId = id; }

	wxUint32 GetRequestedItemId() const { return mReqItemId; }
	wxUint32 GetRequestedItemParentId() const { return mReqItemParentId; }

	//! Returns a pointer to a item that's associated with this event (if any).
	const DatabaseItemBase *GetItem() const { return mpItem; }
protected:
	DatabaseEventBase() : mReqItemId(0), mReqItemParentId(0) { }
private:
	bool mVetoed;

	wxUint32 mReqItemId, mReqItemParentId;

	// The item that's associated with this event (if any).
	const DatabaseItemBase *mpItem;

// public:
	DECLARE_DYNAMIC_CLASS ( DatabaseEventBase )
};

typedef void ( wxEvtHandler::*DatabaseEventFunction )( DatabaseEventBase & );

}

///////////////////////////////////
// Ugly macro-mess ahead.
///////////////////////////////////

DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_ADD, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_CHANGE, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_REQUEST, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_POST, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_COMMIT_START, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_COMMIT_END, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_DELETE, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_RESTORE, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_QUERIED, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_LIST_COMMIT_START, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_LIST_COMMIT_END, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_LIST_QUERY_ALL_START, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_ITEM_LIST_QUERY_ALL_END, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_USER_REQUEST_CLOSE, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_OPEN_START, -1 )
DECLARE_EVENT_TYPE ( COLLIGERE_EVT_DATABASE_OPEN_END, -1 )

#define ColligereDatabaseEventHandlerFunction (wxObjectEventFunction) \
	( wxEventFunction )( col::DatabaseEventFunction )

#define EVT_DATABASE_USER_REQUEST_CLOSE(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_USER_REQUEST_CLOSE, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_USER_REQUEST_CLOSE(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_USER_REQUEST_CLOSE, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_REQUEST(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_REQUEST, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_POST(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        DATABASE_ITEM_POST, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_COMMIT_START(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_COMMIT_START, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ),  

#define EVT_DATABASE_ITEM_COMMIT_END(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_COMMIT_END, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ),  

#define EVT_DATABASE_ITEM_ADD(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_ADD, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ),  

#define EVT_DATABASE_ITEM_CHANGE(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_CHANGE, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_QUERIED_START(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_QUERIED, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_DELETE(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_DELETE, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_RESTORE(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_RESTORE, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_LIST_COMMIT_START(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_LIST_COMMIT_START, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_LIST_COMMIT_END(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_LIST_COMMIT_END, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_LIST_QUERY_ALL_START(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_LIST_QUERY_ALL_START, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_ITEM_LIST_QUERY_ALL_END(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_ITEM_LIST_QUERY_ALL_END, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_OPEN_START(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_OPEN_START, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 

#define EVT_DATABASE_OPEN_END(id, fn) DECLARE_EVENT_TABLE_ENTRY( \
        COLLIGERE_EVT_DATABASE_OPEN_END, id, -1, \
		ColligereDatabaseEventHandlerFunction & fn, \
        ( wxObject * ) NULL ), 